package com.xiangwei.util;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;







import com.xiangwei.entity.Product;
import com.xiangwei.entity.Summary;
import com.xiangwei.service.IGodService;
import com.xiangwei.service.IProductService;
import com.xiangwei.service.IQrcodeService;
import com.xiangwei.service.IScanrecordService;
import com.xiangwei.service.ISummaryService;

/**
 * copyright Portal 2015
 * @brief 定时器类
 */
@Component
public class TimerWorker {
	@Autowired
	private IGodService godService;
	@Autowired
	private IProductService productService;
	@Autowired
	private IQrcodeService qrcodeService;
	@Autowired
	private ISummaryService summaryService;
	@Resource
	private IScanrecordService scanrecordService;
	
    /**
     * 每天晚上12点定时新增summary表中数据
     */
//	@Scheduled(cron = "0 0 0 * * ?")
	@Scheduled(fixedRate= 1000*60*60*24)
    public void changeSignStatus(){
		Summary s = summaryService.selectSummary();
//		System.out.println(s);
//		Integer yesterdayGodCount = 0;
		Integer yesterdayScanrecordCount = 0;
		
		Integer customerCount = productService.getCount();//产品数    		
		Long qrcodeCount = (long) qrcodeService.getQrcodeCount();//总产码数
//		Long godCount = (long) qrcodeService.getGodCount();//总独立扫码用户数    		
		Long qrcodeScanCount = (long) qrcodeService.getScanrecordCount();//总扫码数量
		
		List<Product> pList = productService.getProductAllList();
		Map<String,Object> map = new HashMap<String, Object>();
		Date endDate = DateUtils.addDay(new Date(), -1);
		Date startDate = DateUtils.addDay(endDate, -1);
		map.put("startDate", startDate);
		map.put("endDate", endDate);
		for(Product p : pList){
			map.put("productId", p.getId());  
//			String tableName = "qlm_portal_god_"+p.getId();
			String tableName1 = "qlm_portal_scanrecord_"+p.getId();
			
			if( qrcodeService.existTable(tableName1) > 0){
//				yesterdayGodCount += godService.selectGodByDay(map);
				yesterdayScanrecordCount = scanrecordService.yesterScanrecordCount(map);				
			}
		}
		if(Common.isNullOrEmpty(s)){
			
			//设置参数
			Summary summary = new Summary();
			summary.setCreateTime(DateUtils.getCurrentDate());
			summary.setId(Common.getUUID());
			summary.setCustomerCount(customerCount);
			summary.setQrcodeCount(qrcodeCount);
			summary.setScanQrcodeCount(qrcodeScanCount);
			summary.setYesterdayScanrecordCount(yesterdayScanrecordCount);
			summaryService.insertSelective(summary);          //添加统计数据
			
		}else{
			s.setCreateTime(DateUtils.getCurrentDate());
			s.setCustomerCount(customerCount);
			s.setQrcodeCount(qrcodeCount);
			s.setScanQrcodeCount(qrcodeScanCount);
			s.setYesterdayScanrecordCount(yesterdayScanrecordCount);
			summaryService.updateSelective(s);
		}
		
    }
}
